home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / clang / gnumake.zip / GETLOADA.C < prev    next >
C/C++ Source or Header  |  1994-05-10  |  21KB  |  896 lines

  1. /* Get the system load averages.
  2.    Copyright (C) 1985, 86, 87, 88, 89, 91, 92, 93
  3.        Free Software Foundation, Inc.
  4.  
  5.    This program is free software; you can redistribute it and/or modify
  6.    it under the terms of the GNU General Public License as published by
  7.    the Free Software Foundation; either version 2, or (at your option)
  8.    any later version.
  9.  
  10.    This program is distributed in the hope that it will be useful,
  11.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.    GNU General Public License for more details.
  14.  
  15.    You should have received a copy of the GNU General Public License
  16.    along with this program; if not, write to the Free Software
  17.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. /* Compile-time symbols that this file uses:
  20.  
  21.    FIXUP_KERNEL_SYMBOL_ADDR()    Adjust address in returned struct nlist.
  22.    KERNEL_FILE            Pathname of the kernel to nlist.
  23.    LDAV_CVT()            Scale the load average from the kernel.
  24.                 Returns a double.
  25.    LDAV_SYMBOL            Name of kernel symbol giving load average.
  26.    LOAD_AVE_TYPE        Type of the load average array in the kernel.
  27.                 Must be defined unless one of
  28.                 apollo, DGUX, NeXT, or UMAX is defined;
  29.                 otherwise, no load average is available.
  30.    NLIST_STRUCT            Include nlist.h, not a.out.h, and
  31.                 the nlist n_name element is a pointer,
  32.                 not an array.
  33.    NLIST_NAME_UNION        struct nlist has an n_un member, not n_name.
  34.    LINUX_LDAV_FILE        [__linux__]: File containing load averages.
  35.  
  36.    Specific system predefines this file uses, aside from setting
  37.    default values if not emacs:
  38.  
  39.    apollo
  40.    BSD                Real BSD, not just BSD-like.
  41.    DGUX
  42.    eunice            UNIX emulator under VMS.
  43.    hpux
  44.    NeXT
  45.    sgi
  46.    sequent            Sequent Dynix 3.x.x (BSD)
  47.    _SEQUENT_            Sequent DYNIX/ptx 1.x.x (SYSV)
  48.    sony_news                    NEWS-OS (works at least for 4.1C)
  49.    UMAX
  50.    UMAX4_3
  51.    VMS
  52.    __linux__            Linux: assumes /proc filesystem mounted.
  53.                    Support from Michael K. Johnson.
  54.    __NetBSD__            NetBSD: assumes /kern filesystem mounted.
  55.  
  56.    In addition, to avoid nesting many #ifdefs, we internally set
  57.    LDAV_DONE to indicate that the load average has been computed.
  58.  
  59.    We also #define LDAV_PRIVILEGED if a program will require
  60.    special installation to be able to call getloadavg.  */
  61.  
  62. #include <sys/types.h>
  63.  
  64. /* Both the Emacs and non-Emacs sections want this.  Some
  65.    configuration files' definitions for the LOAD_AVE_CVT macro (like
  66.    sparc.h's) use macros like FSCALE, defined here.  */
  67. #ifdef unix
  68. #include <sys/param.h>
  69. #endif
  70.  
  71.  
  72. #ifdef HAVE_CONFIG_H
  73. #if defined (emacs) || defined (CONFIG_BROKETS)
  74. /* We use <config.h> instead of "config.h" so that a compilation
  75.    using -I. -I$srcdir will use ./config.h rather than $srcdir/config.h
  76.    (which it would do because it found this file in $srcdir).  */
  77. #include <config.h>
  78. #else
  79. #include "config.h"
  80. #endif
  81. #endif
  82.  
  83.  
  84. /* Exclude all the code except the test program at the end
  85.    if the system has its own `getloadavg' function.  */
  86.  
  87. #ifndef HAVE_GETLOADAVG
  88.  
  89.  
  90. /* The existing Emacs configuration files define a macro called
  91.    LOAD_AVE_CVT, which accepts a value of type LOAD_AVE_TYPE, and
  92.    returns the load average multiplied by 100.  What we actually want
  93.    is a macro called LDAV_CVT, which returns the load average as an
  94.    unmultiplied double.
  95.  
  96.    For backwards compatibility, we'll define LDAV_CVT in terms of
  97.    LOAD_AVE_CVT, but future machine config files should just define
  98.    LDAV_CVT directly.  */
  99.  
  100. #if !defined(LDAV_CVT) && defined(LOAD_AVE_CVT)
  101. #define LDAV_CVT(n) (LOAD_AVE_CVT (n) / 100.0)
  102. #endif
  103.  
  104. #if !defined (BSD) && defined (ultrix)
  105. /* Ultrix behaves like BSD on Vaxen.  */
  106. #define BSD
  107. #endif
  108.  
  109. #ifdef NeXT
  110. /* NeXT in the 2.{0,1,2} releases defines BSD in <sys/param.h>, which
  111.    conflicts with the definition understood in this file, that this
  112.    really is BSD. */
  113. #undef BSD
  114.  
  115. /* NeXT defines FSCALE in <sys/param.h>.  However, we take FSCALE being
  116.    defined to mean that the nlist method should be used, which is not true.  */
  117. #undef FSCALE
  118. #endif
  119.  
  120. /* Set values that are different from the defaults, which are
  121.    set a little farther down with #ifndef.  */
  122.  
  123.  
  124. /* Some shorthands.  */
  125.  
  126. #if defined (HPUX) && !defined (hpux)
  127. #define hpux
  128. #endif
  129.  
  130. #if defined(hp300) && !defined(hpux)
  131. #define MORE_BSD
  132. #endif
  133.  
  134. #if defined(ultrix) && defined(mips)
  135. #define decstation
  136. #endif
  137.  
  138. #if defined(sun) && defined(SVR4)
  139. #define SUNOS_5
  140. #endif
  141.  
  142. #if defined (__osf__) && (defined (__alpha) || defined (__alpha__))
  143. #define OSF_ALPHA
  144. #endif
  145.  
  146. #if defined (__osf__) && (defined (mips) || defined (__mips__))
  147. #define OSF_MIPS
  148. #include <sys/table.h>
  149. #endif
  150.  
  151. /* UTek's /bin/cc on the 4300 has no architecture specific cpp define by
  152.    default, but _MACH_IND_SYS_TYPES is defined in <sys/types.h>.  Combine
  153.    that with a couple of other things and we'll have a unique match.  */
  154. #if !defined (tek4300) && defined (unix) && defined (m68k) && defined (mc68000) && defined (mc68020) && defined (_MACH_IND_SYS_TYPES)
  155. #define tek4300            /* Define by emacs, but not by other users.  */
  156. #endif
  157.  
  158.  
  159. /* VAX C can't handle multi-line #ifs, or lines longer than 256 chars.  */
  160. #ifndef LOAD_AVE_TYPE
  161.  
  162. #ifdef MORE_BSD
  163. #define LOAD_AVE_TYPE long
  164. #endif
  165.  
  166. #ifdef sun
  167. #define LOAD_AVE_TYPE long
  168. #endif
  169.  
  170. #ifdef decstation
  171. #define LOAD_AVE_TYPE long
  172. #endif
  173.  
  174. #ifdef _SEQUENT_
  175. #define LOAD_AVE_TYPE long
  176. #endif
  177.  
  178. #ifdef sgi
  179. #define LOAD_AVE_TYPE long
  180. #endif
  181.  
  182. #ifdef SVR4
  183. #define LOAD_AVE_TYPE long
  184. #endif
  185.  
  186. #ifdef sony_news
  187. #define LOAD_AVE_TYPE long
  188. #endif
  189.  
  190. #ifdef sequent
  191. #define LOAD_AVE_TYPE long
  192. #endif
  193.  
  194. #ifdef OSF_ALPHA
  195. #define LOAD_AVE_TYPE long
  196. #endif
  197.  
  198. #if defined (ardent) && defined (titan)
  199. #define LOAD_AVE_TYPE long
  200. #endif
  201.  
  202. #ifdef tek4300
  203. #define LOAD_AVE_TYPE long
  204. #endif
  205.  
  206. #endif /* No LOAD_AVE_TYPE.  */
  207.  
  208. #ifdef OSF_ALPHA
  209. /* <sys/param.h> defines an incorrect value for FSCALE on Alpha OSF/1,
  210.    according to ghazi@noc.rutgers.edu.  */
  211. #undef FSCALE
  212. #define FSCALE 1024.0
  213. #endif
  214.  
  215.  
  216. #ifndef    FSCALE
  217.  
  218. /* SunOS and some others define FSCALE in sys/param.h.  */
  219.  
  220. #ifdef MORE_BSD
  221. #define FSCALE 2048.0
  222. #endif
  223.  
  224. #if defined(MIPS) || defined(SVR4) || defined(decstation)
  225. #define FSCALE 256
  226. #endif
  227.  
  228. #if defined (sgi) || defined (sequent)
  229. /* Sometimes both MIPS and sgi are defined, so FSCALE was just defined
  230.    above under #ifdef MIPS.  But we want the sgi value.  */
  231. #undef FSCALE
  232. #define    FSCALE 1000.0
  233. #endif
  234.  
  235. #if defined (ardent) && defined (titan)
  236. #define FSCALE 65536.0
  237. #endif
  238.  
  239. #ifdef tek4300
  240. #define FSCALE 100.0
  241. #endif
  242.  
  243. #endif    /* Not FSCALE.  */
  244.  
  245. #if !defined (LDAV_CVT) && defined (FSCALE)
  246. #define    LDAV_CVT(n) (((double) (n)) / FSCALE)
  247. #endif
  248.  
  249. /* VAX C can't handle multi-line #ifs, or lines longer that 256 characters.  */
  250. #ifndef NLIST_STRUCT
  251.  
  252. #ifdef MORE_BSD
  253. #define NLIST_STRUCT
  254. #endif
  255.  
  256. #ifdef sun
  257. #define NLIST_STRUCT
  258. #endif
  259.  
  260. #ifdef decstation
  261. #define NLIST_STRUCT
  262. #endif
  263.  
  264. #ifdef hpux
  265. #define NLIST_STRUCT
  266. #endif
  267.  
  268. #if defined (_SEQUENT_) || defined (sequent)
  269. #define NLIST_STRUCT
  270. #endif
  271.  
  272. #ifdef sgi
  273. #define NLIST_STRUCT
  274. #endif
  275.  
  276. #ifdef SVR4
  277. #define NLIST_STRUCT
  278. #endif
  279.  
  280. #ifdef sony_news
  281. #define NLIST_STRUCT
  282. #endif
  283.  
  284. #ifdef OSF_ALPHA
  285. #define NLIST_STRUCT
  286. #endif
  287.  
  288. #if defined (ardent) && defined (titan)
  289. #define NLIST_STRUCT
  290. #endif
  291.  
  292. #ifdef tex4300
  293. #define NLIST_STRUCT
  294. #endif
  295.  
  296. #ifdef butterfly
  297. #define NLIST_STRUCT
  298. #endif
  299.  
  300. #endif /* defined (NLIST_STRUCT) */
  301.  
  302.  
  303. #if defined(sgi) || (defined(mips) && !defined(BSD))
  304. #define FIXUP_KERNEL_SYMBOL_ADDR(nl) ((nl)[0].n_value &= ~(1 << 31))
  305. #endif
  306.  
  307.  
  308. #if !defined (KERNEL_FILE) && defined (sequent)
  309. #define KERNEL_FILE "/dynix"
  310. #endif
  311.  
  312. #if !defined (KERNEL_FILE) && defined (hpux)
  313. #define KERNEL_FILE "/hp-ux"
  314. #endif
  315.  
  316. #if !defined(KERNEL_FILE) && (defined(_SEQUENT_) || defined(MIPS) || defined(SVR4) || defined(ISC) || defined (sgi) || defined(SVR4) || (defined (ardent) && defined (titan)))
  317. #define KERNEL_FILE "/unix"
  318. #endif
  319.  
  320.  
  321. #if !defined (LDAV_SYMBOL) && defined (alliant)
  322. #define LDAV_SYMBOL "_Loadavg"
  323. #endif
  324.  
  325. #if !defined(LDAV_SYMBOL) && ((defined(hpux) && !defined(hp9000s300)) || defined(_SEQUENT_) || defined(SVR4) || defined(ISC) || defined(sgi) || (defined (ardent) && defined (titan)))
  326. #define LDAV_SYMBOL "avenrun"
  327. #endif
  328.  
  329. #ifdef HAVE_UNISTD_H
  330. #include <unistd.h>
  331. #endif
  332.  
  333. #include <stdio.h>
  334. #include <errno.h>
  335.  
  336. #ifndef errno
  337. extern int errno;
  338. #endif
  339.  
  340. /* LOAD_AVE_TYPE should only get defined if we're going to use the
  341.    nlist method.  */
  342. #if !defined(LOAD_AVE_TYPE) && (defined(BSD) || defined(LDAV_CVT) || defined(KERNEL_FILE) || defined(LDAV_SYMBOL))
  343. #define LOAD_AVE_TYPE double
  344. #endif
  345.  
  346. #ifdef LOAD_AVE_TYPE
  347.  
  348. #ifndef VMS
  349. #ifndef NLIST_STRUCT
  350. #include <a.out.h>
  351. #else /* NLIST_STRUCT */
  352. #include <nlist.h>
  353. #endif /* NLIST_STRUCT */
  354.  
  355. #ifdef SUNOS_5
  356. #include <fcntl.h>
  357. #include <kvm.h>
  358. #endif
  359.  
  360. #ifndef KERNEL_FILE
  361. #define KERNEL_FILE "/vmunix"
  362. #endif /* KERNEL_FILE */
  363.  
  364. #ifndef LDAV_SYMBOL
  365. #define LDAV_SYMBOL "_avenrun"
  366. #endif /* LDAV_SYMBOL */
  367.  
  368. #else /* VMS */
  369.  
  370. #ifndef eunice
  371. #include <iodef.h>
  372. #include <descrip.h>
  373. #else /* eunice */
  374. #include <vms/iodef.h>
  375. #endif /* eunice */
  376. #endif /* VMS */
  377.  
  378. #ifndef LDAV_CVT
  379. #define LDAV_CVT(n) ((double) (n))
  380. #endif /* !LDAV_CVT */
  381.  
  382. #endif /* LOAD_AVE_TYPE */
  383.  
  384. #ifdef NeXT
  385. #ifdef HAVE_MACH_MACH_H
  386. #include <mach/mach.h>
  387. #else
  388. #include <mach.h>
  389. #endif
  390. #endif /* NeXT */
  391.  
  392. #ifdef sgi
  393. #include <sys/sysmp.h>
  394. #endif /* sgi */
  395.  
  396. #ifdef UMAX
  397. #include <stdio.h>
  398. #include <signal.h>
  399. #include <sys/time.h>
  400. #include <sys/wait.h>
  401. #include <sys/syscall.h>
  402.  
  403. #ifdef UMAX_43
  404. #include <machine/cpu.h>
  405. #include <inq_stats/statistics.h>
  406. #include <inq_stats/sysstats.h>
  407. #include <inq_stats/cpustats.h>
  408. #include <inq_stats/procstats.h>
  409. #else /* Not UMAX_43.  */
  410. #include <sys/sysdefs.h>
  411. #include <sys/statistics.h>
  412. #include <sys/sysstats.h>
  413. #include <sys/cpudefs.h>
  414. #include <sys/cpustats.h>
  415. #include <sys/procstats.h>
  416. #endif /* Not UMAX_43.  */
  417. #endif /* UMAX */
  418.  
  419. #ifdef DGUX
  420. #include <sys/dg_sys_info.h>
  421. #endif
  422.  
  423. #if defined(HAVE_FCNTL_H) || defined(_POSIX_VERSION)
  424. #include <fcntl.h>
  425. #else
  426. #include <sys/file.h>
  427. #endif
  428.  
  429. /* Avoid static vars inside a function since in HPUX they dump as pure.  */
  430.  
  431. #ifdef NeXT
  432. static processor_set_t default_set;
  433. static int getloadavg_initialized;
  434. #endif /* NeXT */
  435.  
  436. #ifdef UMAX
  437. static unsigned int cpus = 0;
  438. static unsigned int samples;
  439. #endif /* UMAX */
  440.  
  441. #ifdef DGUX
  442. static struct dg_sys_info_load_info load_info;    /* what-a-mouthful! */
  443. #endif /* DGUX */
  444.  
  445. #ifdef LOAD_AVE_TYPE
  446. /* File descriptor open to /dev/kmem or VMS load ave driver.  */
  447. static int channel;
  448. /* Nonzero iff channel is valid.  */
  449. static int getloadavg_initialized;
  450. /* Offset in kmem to seek to read load average, or 0 means invalid.  */
  451. static long offset;
  452.  
  453. #if !defined(VMS) && !defined(sgi)
  454. static struct nlist nl[2];
  455. #endif /* Not VMS or sgi */
  456.  
  457. #ifdef SUNOS_5
  458. static kvm_t *kd;
  459. #endif /* SUNOS_5 */
  460.  
  461. #endif /* LOAD_AVE_TYPE */
  462.  
  463. /* Put the 1 minute, 5 minute and 15 minute load averages
  464.    into the first NELEM elements of LOADAVG.
  465.    Return the number written (never more than 3, but may be less than NELEM),
  466.    or -1 if an error occurred.  */
  467.  
  468. int
  469. getloadavg (loadavg, nelem)
  470.      double loadavg[];
  471.      int nelem;
  472. {
  473.   int elem = 0;            /* Return value.  */
  474.  
  475. #ifdef NO_GET_LOAD_AVG
  476. #define LDAV_DONE
  477.   /* Set errno to zero to indicate that there was no particular error;
  478.      this function just can't work at all on this system.  */
  479.   errno = 0;
  480.   elem = -1;
  481. #endif
  482.  
  483. #if !defined (LDAV_DONE) && defined (__linux__)
  484. #define LDAV_DONE
  485. #undef LOAD_AVE_TYPE
  486.  
  487. #ifndef LINUX_LDAV_FILE
  488. #define LINUX_LDAV_FILE "/proc/loadavg"
  489. #endif
  490.  
  491.   char ldavgbuf[40];
  492.   double load_ave[3];
  493.   int fd, count;
  494.  
  495.   fd = open (LINUX_LDAV_FILE, O_RDONLY);
  496.   if (fd == -1)
  497.     return -1;
  498.   count = read (fd, ldavgbuf, 40);
  499.   (void) close (fd);
  500.   if (count <= 0)
  501.     return -1;
  502.  
  503.   count = sscanf (ldavgbuf, "%lf %lf %lf",
  504.           &load_ave[0], &load_ave[1], &load_ave[2]);
  505.   if (count < 1)
  506.     return -1;
  507.  
  508.   for (elem = 0; elem < nelem && elem < count; elem++)
  509.     loadavg[elem] = load_ave[elem];
  510.  
  511.   return elem;
  512.  
  513. #endif /* __linux__ */
  514.  
  515. #if !defined (LDAV_DONE) && defined (__NetBSD__)
  516. #define LDAV_DONE
  517. #undef LOAD_AVE_TYPE
  518.  
  519. #ifndef NETBSD_LDAV_FILE
  520. #define NETBSD_LDAV_FILE "/kern/loadavg"
  521. #endif
  522.  
  523.   unsigned long int load_ave[3], scale;
  524.   int count;
  525.   FILE *fp;
  526.  
  527.   fp = fopen (NETBSD_LDAV_FILE, "r");
  528.   if (fp == NULL)
  529.     return -1;
  530.   count = fscanf (fp, "%lu %lu %lu %lu\n",
  531.           &load_ave[0], &load_ave[1], &load_ave[2],
  532.           &scale);
  533.   (void) fclose (fp);
  534.   if (count != 4)
  535.     return -1;
  536.  
  537.   for (elem = 0; elem < nelem; elem++)
  538.     loadavg[elem] = (double) load_ave[elem] / (double) scale;
  539.  
  540.   return elem;
  541.  
  542. #endif /* __NetBSD__ */
  543.  
  544. #if !defined (LDAV_DONE) && defined (NeXT)
  545. #define LDAV_DONE
  546.   /* The NeXT code was adapted from iscreen 3.2.  */
  547.  
  548.   host_t host;
  549.   struct processor_set_basic_info info;
  550.   unsigned info_count;
  551.  
  552.   /* We only know how to get the 1-minute average for this system,
  553.      so even if the caller asks for more than 1, we only return 1.  */
  554.  
  555.   if (!getloadavg_initialized)
  556.     {
  557.       if (processor_set_default (host_self (), &default_set) == KERN_SUCCESS)
  558.     getloadavg_initialized = 1;
  559.     }
  560.  
  561.   if (getloadavg_initialized)
  562.     {
  563.       info_count = PROCESSOR_SET_BASIC_INFO_COUNT;
  564.       if (processor_set_info (default_set, PROCESSOR_SET_BASIC_INFO, &host,
  565.                  (processor_set_info_t) &info, &info_count)
  566.       != KERN_SUCCESS)
  567.     getloadavg_initialized = 0;
  568.       else
  569.     {
  570.       if (nelem > 0)
  571.         loadavg[elem++] = (double) info.load_average / LOAD_SCALE;
  572.     }
  573.     }
  574.  
  575.   if (!getloadavg_initialized)
  576.     return -1;
  577. #endif /* NeXT */
  578.  
  579. #if !defined (LDAV_DONE) && defined (UMAX)
  580. #define LDAV_DONE
  581. /* UMAX 4.2, which runs on the Encore Multimax multiprocessor, does not
  582.    have a /dev/kmem.  Information about the workings of the running kernel
  583.    can be gathered with inq_stats system calls.
  584.    We only know how to get the 1-minute average for this system.  */
  585.  
  586.   struct proc_summary proc_sum_data;
  587.   struct stat_descr proc_info;
  588.   double load;
  589.   register unsigned int i, j;
  590.  
  591.   if (cpus == 0)
  592.     {
  593.       register unsigned int c, i;
  594.       struct cpu_config conf;
  595.       struct stat_descr desc;
  596.  
  597.       desc.sd_next = 0;
  598.       desc.sd_subsys = SUBSYS_CPU;
  599.       desc.sd_type = CPUTYPE_CONFIG;
  600.       desc.sd_addr = (char *) &conf;
  601.       desc.sd_size = sizeof conf;
  602.  
  603.       if (inq_stats (1, &desc))
  604.     return -1;
  605.  
  606.       c = 0;
  607.       for (i = 0; i < conf.config_maxclass; ++i)
  608.     {
  609.       struct class_stats stats;
  610.       bzero ((char *) &stats, sizeof stats);
  611.  
  612.       desc.sd_type = CPUTYPE_CLASS;
  613.       desc.sd_objid = i;
  614.       desc.sd_addr = (char *) &stats;
  615.       desc.sd_size = sizeof stats;
  616.  
  617.       if (inq_stats (1, &desc))
  618.         return -1;
  619.  
  620.       c += stats.class_numcpus;
  621.     }
  622.       cpus = c;
  623.       samples = cpus < 2 ? 3 : (2 * cpus / 3);
  624.     }
  625.  
  626.   proc_info.sd_next = 0;
  627.   proc_info.sd_subsys = SUBSYS_PROC;
  628.   proc_info.sd_type = PROCTYPE_SUMMARY;
  629.   proc_info.sd_addr = (char *) &proc_sum_data;
  630.   proc_info.sd_size = sizeof (struct proc_summary);
  631.   proc_info.sd_sizeused = 0;
  632.  
  633.   if (inq_stats (1, &proc_info) != 0)
  634.     return -1;
  635.  
  636.   load = proc_sum_data.ps_nrunnable;
  637.   j = 0;
  638.   for (i = samples - 1; i > 0; --i)
  639.     {
  640.       load += proc_sum_data.ps_nrun[j];
  641.       if (j++ == PS_NRUNSIZE)
  642.     j = 0;
  643.     }
  644.  
  645.   if (nelem > 0)
  646.     loadavg[elem++] = load / samples / cpus;
  647. #endif /* UMAX */
  648.  
  649. #if !defined (LDAV_DONE) && defined (DGUX)
  650. #define LDAV_DONE
  651.   /* This call can return -1 for an error, but with good args
  652.      it's not supposed to fail.  The first argument is for no
  653.      apparent reason of type `long int *'.  */
  654.   dg_sys_info ((long int *) &load_info,
  655.            DG_SYS_INFO_LOAD_INFO_TYPE,
  656.            DG_SYS_INFO_LOAD_VERSION_0);
  657.  
  658.   if (nelem > 0)
  659.     loadavg[elem++] = load_info.one_minute;
  660.   if (nelem > 1)
  661.     loadavg[elem++] = load_info.five_minute;
  662.   if (nelem > 2)
  663.     loadavg[elem++] = load_info.fifteen_minute;
  664. #endif /* DGUX */
  665.  
  666. #if !defined (LDAV_DONE) && defined (apollo)
  667. #define LDAV_DONE
  668. /* Apollo code from lisch@mentorg.com (Ray Lischner).
  669.  
  670.    This system call is not documented.  The load average is obtained as
  671.    three long integers, for the load average over the past minute,
  672.    five minutes, and fifteen minutes.  Each value is a scaled integer,
  673.    with 16 bits of integer part and 16 bits of fraction part.
  674.  
  675.    I'm not sure which operating system first supported this system call,
  676.    but I know that SR10.2 supports it.  */
  677.  
  678.   extern void proc1_$get_loadav ();
  679.   unsigned long load_ave[3];
  680.  
  681.   proc1_$get_loadav (load_ave);
  682.  
  683.   if (nelem > 0)
  684.     loadavg[elem++] = load_ave[0] / 65536.0;
  685.   if (nelem > 1)
  686.     loadavg[elem++] = load_ave[1] / 65536.0;
  687.   if (nelem > 2)
  688.     loadavg[elem++] = load_ave[2] / 65536.0;
  689. #endif /* apollo */
  690.  
  691. #if !defined (LDAV_DONE) && defined (OSF_MIPS)
  692. #define LDAV_DONE
  693.  
  694.   struct tbl_loadavg load_ave;
  695.   table (TBL_LOADAVG, 0, &load_ave, 1, sizeof (load_ave));
  696.   loadavg[elem++]
  697.     = (load_ave.tl_lscale == 0
  698.        ? load_ave.tl_avenrun.d[0]
  699.        : (load_ave.tl_avenrun.l[0] / (double) load_ave.tl_lscale));
  700. #endif    /* OSF_MIPS */
  701.  
  702. #if !defined (LDAV_DONE) && defined (VMS)
  703.   /* VMS specific code -- read from the Load Ave driver.  */
  704.  
  705.   LOAD_AVE_TYPE load_ave[3];
  706.   static int getloadavg_initialized = 0;
  707. #ifdef eunice
  708.   struct
  709.   {
  710.     int dsc$w_length;
  711.     char *dsc$a_pointer;
  712.   } descriptor;
  713. #endif
  714.  
  715.   /* Ensure that there is a channel open to the load ave device.  */
  716.   if (!getloadavg_initialized)
  717.     {
  718.       /* Attempt to open the channel.  */
  719. #ifdef eunice
  720.       descriptor.dsc$w_length = 18;
  721.       descriptor.dsc$a_pointer = "$$VMS_LOAD_AVERAGE";
  722. #else
  723.       $DESCRIPTOR (descriptor, "LAV0:");
  724. #endif
  725.       if (sys$assign (&descriptor, &channel, 0, 0) & 1)
  726.     getloadavg_initialized = 1;
  727.     }
  728.  
  729.   /* Read the load average vector.  */
  730.   if (getloadavg_initialized
  731.       && !(sys$qiow (0, channel, IO$_READVBLK, 0, 0, 0,
  732.              load_ave, 12, 0, 0, 0, 0) & 1))
  733.     {
  734.       sys$dassgn (channel);
  735.       getloadavg_initialized = 0;
  736.     }
  737.  
  738.   if (!getloadavg_initialized)
  739.     return -1;
  740. #endif /* VMS */
  741.  
  742. #if !defined (LDAV_DONE) && defined(LOAD_AVE_TYPE) && !defined(VMS)
  743.  
  744.   /* UNIX-specific code -- read the average from /dev/kmem.  */
  745.  
  746. #define LDAV_PRIVILEGED        /* This code requires special installation.  */
  747.  
  748.   LOAD_AVE_TYPE load_ave[3];
  749.  
  750.   /* Get the address of LDAV_SYMBOL.  */
  751.   if (offset == 0)
  752.     {
  753. #ifndef sgi
  754. #ifndef NLIST_STRUCT
  755.       strcpy (nl[0].n_name, LDAV_SYMBOL);
  756.       strcpy (nl[1].n_name, "");
  757. #else /* NLIST_STRUCT */
  758. #ifdef NLIST_NAME_UNION
  759.       nl[0].n_un.n_name = LDAV_SYMBOL;
  760.       nl[1].n_un.n_name = 0;
  761. #else /* not NLIST_NAME_UNION */
  762.       nl[0].n_name = LDAV_SYMBOL;
  763.       nl[1].n_name = 0;
  764. #endif /* not NLIST_NAME_UNION */
  765. #endif /* NLIST_STRUCT */
  766.  
  767. #ifndef SUNOS_5
  768.       if (nlist (KERNEL_FILE, nl) >= 0)
  769.     /* Omit "&& nl[0].n_type != 0 " -- it breaks on Sun386i.  */
  770.     {
  771. #ifdef FIXUP_KERNEL_SYMBOL_ADDR
  772.       FIXUP_KERNEL_SYMBOL_ADDR (nl);
  773. #endif
  774.       offset = nl[0].n_value;
  775.     }
  776. #endif  /* !SUNOS_5 */
  777. #else /* sgi */
  778.       int ldav_off;
  779.  
  780.       ldav_off = sysmp (MP_KERNADDR, MPKA_AVENRUN);
  781.       if (ldav_off != -1)
  782.     offset = (long) ldav_off & 0x7fffffff;
  783. #endif /* sgi */
  784.     }
  785.  
  786.   /* Make sure we have /dev/kmem open.  */
  787.   if (!getloadavg_initialized)
  788.     {
  789. #ifndef SUNOS_5
  790.       channel = open ("/dev/kmem", 0);
  791.       if (channel >= 0)
  792.     getloadavg_initialized = 1;
  793. #else /* SUNOS_5 */
  794.       /* We pass 0 for the kernel, corefile, and swapfile names
  795.      to use the currently running kernel.  */
  796.       kd = kvm_open (0, 0, 0, O_RDONLY, 0);
  797.       if (kd != 0) 
  798.     {
  799.       /* nlist the currently running kernel.  */
  800.       kvm_nlist (kd, nl);
  801.       offset = nl[0].n_value;
  802.       getloadavg_initialized = 1;
  803.     }
  804. #endif /* SUNOS_5 */
  805.     }
  806.  
  807.   /* If we can, get the load average values.  */
  808.   if (offset && getloadavg_initialized)
  809.     {
  810.       /* Try to read the load.  */
  811. #ifndef SUNOS_5
  812.       if (lseek (channel, offset, 0) == -1L
  813.       || read (channel, (char *) load_ave, sizeof (load_ave))
  814.       != sizeof (load_ave))
  815.     {
  816.       close (channel);
  817.       getloadavg_initialized = 0;
  818.     }
  819. #else  /* SUNOS_5 */
  820.       if (kvm_read (kd, offset, (char *) load_ave, sizeof (load_ave))
  821.       != sizeof (load_ave))
  822.         {
  823.           kvm_close (kd);
  824.           getloadavg_initialized = 0;
  825.     }
  826. #endif /* SUNOS_5 */
  827.     }
  828.  
  829.   if (offset == 0 || !getloadavg_initialized)
  830.     return -1;
  831. #endif /* LOAD_AVE_TYPE and not VMS */
  832.  
  833. #if !defined (LDAV_DONE) && defined (LOAD_AVE_TYPE) /* Including VMS.  */
  834.   if (nelem > 0)
  835.     loadavg[elem++] = LDAV_CVT (load_ave[0]);
  836.   if (nelem > 1)
  837.     loadavg[elem++] = LDAV_CVT (load_ave[1]);
  838.   if (nelem > 2)
  839.     loadavg[elem++] = LDAV_CVT (load_ave[2]);
  840.  
  841. #define LDAV_DONE
  842. #endif /* !LDAV_DONE && LOAD_AVE_TYPE */
  843.  
  844. #ifdef LDAV_DONE
  845.   return elem;
  846. #else
  847.   /* Set errno to zero to indicate that there was no particular error;
  848.      this function just can't work at all on this system.  */
  849.   errno = 0;
  850.   return -1;
  851. #endif
  852. }
  853.  
  854. #endif /* ! HAVE_GETLOADAVG */
  855.  
  856. #ifdef TEST
  857. void
  858. main (argc, argv)
  859.      int argc;
  860.      char **argv;
  861. {
  862.   int naptime = 0;
  863.  
  864.   if (argc > 1)
  865.     naptime = atoi (argv[1]);
  866.  
  867.   while (1)
  868.     {
  869.       double avg[3];
  870.       int loads;
  871.  
  872.       errno = 0;        /* Don't be misled if it doesn't set errno.  */
  873.       loads = getloadavg (avg, 3);
  874.       if (loads == -1)
  875.     {
  876.       perror ("Error getting load average");
  877.       exit (1);
  878.     }
  879.       if (loads > 0)
  880.     printf ("1-minute: %f  ", avg[0]);
  881.       if (loads > 1)
  882.     printf ("5-minute: %f  ", avg[1]);
  883.       if (loads > 2)
  884.     printf ("15-minute: %f  ", avg[2]);
  885.       if (loads > 0)
  886.     putchar ('\n');
  887.  
  888.       if (naptime == 0)
  889.     break;
  890.       sleep (naptime);
  891.     }
  892.  
  893.   exit (0);
  894. }
  895. #endif /* TEST */
  896.